
//获取应用实例
import network from '../../utils/network.js';
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
      data: {
        login: false,
        isuser: 0,
        encry: [],
        userInfo: [],
      },
      onShow: function () {
        app.editTabBar();    //显示自定义的底部导航  
      },  


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
      var that =  this; 
      wx.getStorage({
        key: 'user',
        success: function (res) {
            that.setData({
              userInfo: res.data,
              login: true,
            });
        },
        error: function (e) {
          wx.login({
            success: function (re) {
              that.setData({
                code: re.code
              });
              that.onGetUsrCode("正在加载数据");
            }
          });
        }
      }); 
    },

    onReady() {
      var that = this; 
    },
  /*
 *获取用户授权信息
 */
  onGotUserInfo() {
    var that = this;
    wx.login({
      success: function (re) {
        that.setData({
          code: re.code
        });
        wx.getUserInfo({
          success: function (re) {
            that.setData({
              userInfo: {
                avatar: re.avatarUrl,
                nickName: re.nickName,
              },
              encry: re
            });
            that.onGetUserInfo("正在授权中");
          }
        });
      }
    }); 
  },
    
  onGetUsrCode: function (message) {
    var that = this;
    var data = {
      code: that.data.code,
    };
    network.requestLoading('http://jbc.com/login/Login/getCode', data, message, function (res) {
      if (res.code > 0) {
        wx.setStorage({
          key: 'user',
          data: res.data
        })
        that.setData({
          login: true,
          userInfo: res.data
        });
        //that.getSale_list('正在刷新数据')
      } else {
        // wx.showToast({
        //   title: res.msg,
        // });
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })
  },

  //授权新增用户
  onGetUserInfo: function (message) {
    var that = this;
    var encry = that.data.encry;
    var code = that.data.code;
    var data = {
      code: that.data.code,
      code: code,
      signature: encry.signature,
      rawData: encry.rawData,
      encryptData: encry.encryptedData,
      iv: encry.iv
    };
    network.requestLoading('http://jbc.com/login/Login/getUserInfo', data, message, function (res) {
      if (res.code > 0) { 
        that.setData({
          login: 1,
          userInfo: res.data,
        });
        wx.setStorage({
          key: 'user',
          data: res.data
        })
        //设置标题
        wx.setNavigationBarTitle({
          title: "用户中心"
        });
      } else {
        wx.showToast({
          title: res.msg,
        });
      }
    }, function (res) {
      wx.showToast({

        title: '加载数据失败',
      })

    })
  },

})
